package com.ymatou.apigateway.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.GetproductDetailBean;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.parameter.ProductInfo;
import com.ymatou.apigateway.service.GetproductDetailCall;
import com.ymatou.apigateway.service.OpenapiService;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

import net.sf.json.JSONObject;
/**
*获取单个商品详细信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetproductDetail{
	
	private static OpenapiReq openapiReq;
	private static GetproductDetailBean getproductdetailBean;
	private static GetproductDetailCall getproductdetailCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取单个商品详细信息");
	}

	@Before
	public void caseUp() {
		openapiReq = new OpenapiReq();
		openapiReq.setMethod("ymatou.product.detail.get");

		getproductdetailBean=new GetproductDetailBean();
		getproductdetailCall=new GetproductDetailCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getproductdetailCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetproductDetail_001() {
		Logger.start(true,"获取单个商品详细信息");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
			String sellerName = EnvSetup.getData("productsellername");

			org.json.JSONObject addNewProductOnSaleObject = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null);
			String productID =  addNewProductOnSaleObject.getJSONObject("Data").getString("ProductID");
//			String productID ="883a4206-957f-409a-86e3-6aa734d1bade";
			Thread.sleep(1000);
			
			getproductdetailBean.setProduct_id(productID);
			
			OpenApiWapper openApiWapper = new OpenApiWapper();

			Application application = openApiWapper
					.selectApplicationBySellerId(Long.valueOf(EnvSetup.getData("productsellerid"))).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(getproductdetailBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.product.detail.get",
							app_id);
			getproductdetailCall.setUrl(url);
		
			getproductdetailCall.setData(openapiReq);
			getproductdetailCall.callService();	
			
			Logger.verifyEquals("0000", getproductdetailCall.getCode(), "验证返回Code");
			
			ProductInfo info1=getproductdetailCall.getProductInfo();
			YmtProductsIWapper wapper = new YmtProductsIWapper();			
//			List<Map> info2 = wapper.getSellerProductDetailList(productID, sellerId, 1, 10);
			Map product = wapper.getSellerProductDetailList_product(productID).get(0);
			Map catalogs = wapper.getSellerProductDetailList_Catalogs(productID,sellerId).get(0);
//			Map catalogProperty = wapper.getSellerProductDetailList_CatalogProperty(productID).get(0);
			Map productPicture = wapper.getSellerProductDetailList_ProductPicture(productID).get(0);

			Logger.verifyEquals(product.get("CategoryName").toString(), info1.getCategory_name(), "验证返回category_name");
			Logger.verifyEquals("www.ymatou.com/product/"+productID+".html", info1.getProduct_url(), "验证返回product_url");
			Logger.verifyEquals(product.get("AddTime").toString().subSequence(0, 19), info1.getCreate_time(), "验证返回create_time");
			Logger.verifyEquals(product.get("ValidStart").toString().subSequence(0, 19), info1.getListing_start_time(), "验证返回listing_start_time");
			Logger.verifyEquals(product.get("BrandName").toString(), info1.getBrand_name(), "验证返回brand_name");
			Logger.verifyEquals(product.get("ProductName").toString(), info1.getProduct_name(), "验证返回product_name");
			Logger.verifyEquals(Integer.parseInt(product.get("BrandId").toString()), info1.getBrand_id(), "验证返回brand_id");
			Logger.verifyEquals(productPicture.get("PicUrl").toString(), info1.getProduct_images()[0], "验证返回product_images");
			Logger.verifyEquals(product.get("UpdateTime").toString().subSequence(0, 19), info1.getUpdate_time(), "验证返回update_time");
			Logger.verifyEquals(Integer.parseInt(product.get("CategoryId").toString()), info1.getCategory_id(), "验证返回category_id");
			Logger.verifyEquals(Integer.parseInt(product.get("CatalogStatus").toString()), info1.getDelivery_type(), "验证返回delivery_type");
			Logger.verifyEquals(product.get("ProductId").toString(), info1.getProduct_id(), "验证返回product_id");
			Logger.verifyEquals(product.get("ValidEnd").toString().subSequence(0, 19), info1.getListing_end_time(), "验证返回listing_end_time");
			Logger.verifyEquals(Integer.parseInt(product.get("CatalogType").toString())==2, info1.isFbx(), "验证返回fbx");
			
			//skus
			Logger.verifyEquals(Integer.parseInt(catalogs.get("WeightUnit").toString()), info1.getSkus().get(0).getWeight_unit(), "验证返回weight_unit");
			Logger.verifyEquals(0, info1.getSkus().get(0).getPrice().compareTo(BigDecimal.valueOf(Float.parseFloat(catalogs.get("Price").toString()))), "验证返回price");
			Logger.verifyEquals(Integer.parseInt(catalogs.get("StockNum").toString()), info1.getSkus().get(0).getStock_num(), "验证返回stock_num");
			Logger.verifyEquals(0, info1.getSkus().get(0).getWeight().compareTo(BigDecimal.valueOf(Float.parseFloat(catalogs.get("Weight").toString()))), "验证返回weight");
			Logger.verifyEquals(0, info1.getSkus().get(0).getNew_price().compareTo(BigDecimal.valueOf(Float.parseFloat(catalogs.get("NewPrice").toString()))), "验证返回new_price");
			Logger.verifyEquals(catalogs.get("CatalogId").toString(), info1.getSkus().get(0).getSku_id(), "验证返回sku_id");
			Logger.verifyEquals(0, info1.getSkus().get(0).getVip_price().compareTo(BigDecimal.valueOf(Float.parseFloat(catalogs.get("VipPrice").toString()))), "验证返回vip_price");
			Logger.verifyEquals(catalogs.get("SKU").toString(), info1.getSkus().get(0).getOuter_id(), "验证返回outer_id");
			Logger.verifyEquals(null, info1.getSkus().get(0).getProperties(), "验证返回properties");
			Logger.verifyEquals(Integer.parseInt(catalogs.get("Action").toString())==0, info1.getSkus().get(0).getUsed(), "验证返回is_used");


		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetproductDetail_002() {
		Logger.start(true,"商品Idnull");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
//			String sellerName = EnvSetup.getData("productsellername");
//
//			org.json.JSONObject addNewProductOnSaleObject = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null);
//			String productID =  addNewProductOnSaleObject.getJSONObject("Data").getString("ProductID");
			String productID ="883a4206-957f-409a-86e3-6aa734d1bade";
			Thread.sleep(1000);
			
//			getproductdetailBean.setProduct_id(productID);
			
			OpenApiWapper openApiWapper = new OpenApiWapper();

			Application application = openApiWapper
					.selectApplicationBySellerId(Long.valueOf(EnvSetup.getData("productsellerid"))).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(getproductdetailBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.product.detail.get",
							app_id);
			getproductdetailCall.setUrl(url);
		
			getproductdetailCall.setData(openapiReq);
			getproductdetailCall.callService();	
			
			Logger.verifyEquals("0001", getproductdetailCall.getCode(), "验证返回Code");		
			Logger.verifyEquals("productId:may not be empty", getproductdetailCall.getMessage(), "验证返回message");		

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetproductDetail_003() {
		Logger.start(true,"商品Id不存在");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
			String productID ="883a4206-957f-409a-86e3-1bade555";
			Thread.sleep(1000);
			
			getproductdetailBean.setProduct_id(productID);
			
			OpenApiWapper openApiWapper = new OpenApiWapper();

			Application application = openApiWapper
					.selectApplicationBySellerId(Long.valueOf(EnvSetup.getData("productsellerid"))).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(getproductdetailBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.product.detail.get",
							app_id);
			getproductdetailCall.setUrl(url);
		
			getproductdetailCall.setData(openapiReq);
			getproductdetailCall.callService();	
			
			Logger.verifyEquals("0000", getproductdetailCall.getCode(), "验证返回Code");	
			Logger.verifyEquals(null, getproductdetailCall.getProductInfo(), "验证返回ProductInfo");	

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetproductDetail_004() {
		Logger.start(true,"商品Id与Seller不匹配");
		try {
//			int sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
			int sellerId = 288484;
//			String sellerName = EnvSetup.getData("productsellername");
//
//			org.json.JSONObject addNewProductOnSaleObject = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null);
//			String productID =  addNewProductOnSaleObject.getJSONObject("Data").getString("ProductID");
			String productID ="883a4206-957f-409a-86e3-6aa734d1bade";
			Thread.sleep(1000);
			
			getproductdetailBean.setProduct_id(productID);
			
			OpenApiWapper openApiWapper = new OpenApiWapper();

			Application application = openApiWapper
					.selectApplicationBySellerId(Long.valueOf(sellerId)).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(getproductdetailBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.product.detail.get",
							app_id);
			getproductdetailCall.setUrl(url);
		
			getproductdetailCall.setData(openapiReq);
			getproductdetailCall.callService();	
			
			Logger.verifyEquals("0000", getproductdetailCall.getCode(), "验证返回Code");				
			Logger.verifyEquals(null, getproductdetailCall.getProductInfo(), "验证返回ProductInfo");				

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetproductDetail_005() {
		Logger.start(true,"获取单个商品详细信息-stg");
		try {

			int sellerId = 3383;
			String productID1="e123d00a-6668-47ea-a583-ff1f2642a3e1";
			getproductdetailBean.setProduct_id(productID1);
			
			String app_id = "6QcDcCtvV3pJknJUEp";
			String app_secret = "95605832511B6C85DB2D0FA9E3ACBB61FE3E1730322EF9CCA09E7ED5E523721461BEC16080D01798E84218559F4A6177";
			String auth_code = "2teKsa6gaiSUwxsACry9hXIaVsTEXqqV";

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(auth_code);
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(getproductdetailBean));
			
			String url = String
					.format("https://open.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.product.detail.get",
							app_id);
			getproductdetailCall.setUrl(url);
			
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
			
			getproductdetailCall.setData(openapiReq);
			getproductdetailCall.callService();	
			
			Logger.verifyEquals("0000", getproductdetailCall.getCode(), "验证返回Code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}